package com.ruoyi.qywx;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ruoyi.common.utils.DateUtils;
import lombok.Data;

import java.util.Date;

/**
 * @author : hcl
 * @Date : 2023/9/6 14:20
 */
public class TableEntityCreator {

    static String TABLE = "CREATE TABLE `survey_answer_v3` (\n" +
            "  `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户答案id',\n" +
            "  `ext_user_id` varchar(64) unsigned NOT NULL DEFAULT '0' COMMENT '粉丝id',\n" +
            "  `survey_id` bigint(11) unsigned NOT NULL DEFAULT '0' COMMENT '问卷id',\n" +
            "  `mobile` varchar(60) NOT NULL DEFAULT '' COMMENT '手机号码',\n" +
            "  `email` varchar(150) NOT NULL DEFAULT '' COMMENT '邮箱地址',\n" +
            "  `address` varchar(150) NOT NULL DEFAULT '' COMMENT '地址',\n" +
            "  `name` varchar(50) NOT NULL DEFAULT '' COMMENT '姓名',\n" +
            "  `num` int(10) unsigned NOT NULL DEFAULT '1' COMMENT '用户答案编号',\n" +
            "  `answers` mediumtext COMMENT '用户答卷',\n" +
            "  `is_delete` tinyint(4) NOT NULL DEFAULT '0' COMMENT '是否已删除',\n" +
            "  `create_time` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '创建时间',\n" +
            "  `modify_time` datetime NOT NULL DEFAULT '2000-01-01 00:00:00' COMMENT '修改时间',\n" +
            "  PRIMARY KEY (`id`),\n" +
            "  KEY `idx_surveyId` (`survey_id`)\n" +
            ") ENGINE=InnoDB AUTO_INCREMENT=1502 DEFAULT CHARSET=utf8mb4 COMMENT='调研用户答案表';";

    public static void main(String[] args) {
        StringBuilder table = new StringBuilder();
        String[] lines = TABLE.split("\n");
        for (String line : lines) {
            if (line.startsWith("CREATE")) {
                appendTableName(line, table);
            } else {
                if (line.trim().startsWith("PRIMARY KEY")) break;
                String faild = toHumpString(findName(line));
                table.append(nameComment(line));
                if (faild.equals("id")) {
                    table.append("\t@TableId(type = IdType.AUTO)\n\tprivate Long id;\n");
                } else {
                    table.append("\tprivate ")
                            .append(faildType(line))
                            .append(" ")
                            .append(faild)
                            .append(";\n");
                }
            }
        }
        table.append("\n}");
        System.out.println(table);
    }

    private static String nameComment(String line) {
        if (line.toUpperCase().contains("COMMENT")) {
            String comment = line.split("COMMENT")[1].trim();
            return "\t//" + comment.substring(1, comment.length() - 2) + "\n";
        }
        return "";
    }

    private static String faildType(String line) {
        StringBuilder builder = new StringBuilder();
        String str = line.trim();
        String type = str.split(" ")[1];
        if (type.equals("int")) return "Integer";
        if (type.equals("bigint")) return "Long";
        if (type.startsWith("tinyint")) return "Boolean";
        if (type.startsWith("datetime")) return "Date";
        return "String";
    }

    private static void appendTableName(String line, StringBuilder table) {
        String name = findName(line);
        table.append("import com.baomidou.mybatisplus.annotation.IdType;\n" +
                "import com.baomidou.mybatisplus.annotation.TableId;\n" +
                "import com.baomidou.mybatisplus.annotation.TableName;\n" +
                "import lombok.Data;\n\n")
                .append("/**\n" +
                        " * @author : hcl\n" +
                        " * @Date : " + DateUtil.format(new Date(), "yyyy/MM/dd HH:mm:ss") +
                        " \n*/\n")
                .append("@TableName(\"").append(name).append("\")\n@Data\npublic class ");
        table.append(toHumpString(name));
        table.append("{\n");
    }

    static String toHumpString(String str) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '_') {
                c = (char) (str.charAt(++i) - 32);
            }
            builder.append(c);
        }
        return builder.toString();
    }

    static String findName(String line) {
        StringBuilder name = new StringBuilder();
        boolean start = false;
        for (int i = 0; i < line.length(); i++) {
            if (line.charAt(i) == '`') {
                start = !start;
                if (!start) break;
                continue;
            }
            if (start) {
                name.append(line.charAt(i));
            }
        }
        return name.toString();
    }
}
